package com.cyt.work.kwmanagement.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.base.util.StringUtils;
import com.cyt.work.kwmanagement.dao.KwKcMessageDao;
import com.cyt.work.kwmanagement.model.po.KwKcMessage;
import com.cyt.work.kwmanagement.model.po.KwKcSetting;
import com.cyt.work.kwmanagement.model.vo.KwKcMessageVo;
import com.cyt.work.kwmanagement.model.vo.KwKcSettingVo;
import com.cyt.work.kwmanagement.service.KwKcMessageService;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 考场设置详细表-考场设置详细表Service层实现类
 *
 * @author han jun
 * @date 2019-11-26 11:20:58
 */
@Service("kwKcMessageService")
public class KwKcMessageServiceImpl extends BaseServiceImpl<KwKcMessage> implements KwKcMessageService {

    @Resource
    private KwKcMessageDao kwKcMessageDao;

    @Autowired
    public void setBaseDao(KwKcMessageDao kwKcMessageDao) {
        this.baseDao = kwKcMessageDao;
    }

    /**
     * 查询对象集合
     *
     * @param kwKcMessageVo 对象
     * @return List 对象集合
     * @author han jun
     * @date 2019-11-26 11:20:58
     */
    @Override
    public List<KwKcMessage> listExample(KwKcMessageVo kwKcMessageVo) {
        Example example = new Example(KwKcMessage.class);
        example.setOrderByClause(" create_time desc");
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        if (!StringUtils.isEmpty(kwKcMessageVo.getId())){
            criteria.andEqualTo("id", kwKcMessageVo.getId());
        }
        if (!StringUtils.isEmpty(kwKcMessageVo.getApplyId())){
            criteria.andEqualTo("applyId", kwKcMessageVo.getApplyId());
        }
        if (!StringUtils.isEmpty(kwKcMessageVo.getKsId())){
            criteria.andEqualTo("ksId", kwKcMessageVo.getKsId());
        }
        return kwKcMessageDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param kwKcMessageVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:20:58
     */
    @Override
    public ReturnResult page(KwKcMessageVo kwKcMessageVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<KwKcMessage> list = listExample(kwKcMessageVo);
        PageInfo<KwKcMessage> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param kwKcMessageVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:20:58
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(KwKcMessageVo kwKcMessageVo, SysUser sysUser) {
        if (StringUtils.isBlank(kwKcMessageVo.getId())) {
            KwKcMessage kwKcMessage = new KwKcMessage(sysUser);
            kwKcMessage.setApplyId(kwKcMessageVo.getApplyId());
            kwKcMessage.setKsId(kwKcMessageVo.getKsId());
            kwKcMessage.setKcNum(kwKcMessageVo.getKcNum());
            kwKcMessage.setKcStu(kwKcMessageVo.getKcStu());
            kwKcMessage.setKcPlace(kwKcMessageVo.getKcPlace());
            kwKcMessageDao.insert(kwKcMessage);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            KwKcMessage kwKcMessage = kwKcMessageDao.selectByPrimaryKey(kwKcMessageVo.getId());
            kwKcMessage.setApplyId(kwKcMessageVo.getApplyId());
            kwKcMessage.setKsId(kwKcMessageVo.getKsId());
            kwKcMessage.setKcNum(kwKcMessageVo.getKcNum());
            kwKcMessage.setKcStu(kwKcMessageVo.getKcStu());
            kwKcMessage.setKcPlace(kwKcMessageVo.getKcPlace());
            kwKcMessage.setUpdateTime(new Date());
            kwKcMessage.setUpdateUser(sysUser.getId());
            kwKcMessageDao.updateByPrimaryKey(kwKcMessage);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param kwKcMessageVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:20:58
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(KwKcMessageVo kwKcMessageVo, SysUser sysUser) {
        //要修改的字段
        KwKcMessage kwKcMessage = new KwKcMessage();
        kwKcMessage.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        kwKcMessage.setUpdateUser(sysUser.getId());
        kwKcMessage.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(KwKcMessage.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(kwKcMessageVo.getId())) {
            criteria.andIn("id", Arrays.asList(kwKcMessageVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        kwKcMessageDao.updateByExampleSelective(kwKcMessage, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param kwKcMessageVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author han jun
     * @date 2019-11-26 11:20:58
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(KwKcMessageVo kwKcMessageVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(KwKcMessage.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(kwKcMessageVo.getId())) {
            criteria.andIn("id", Arrays.asList(kwKcMessageVo.getId().split(",")));
        }
        kwKcMessageDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }


    /**
     * 删除的考场详情
     *
     * @param kwKcMessageVo  参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author Jun Han
     * @date 2019/12/4 16:51
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult removeAll(KwKcMessageVo kwKcMessageVo, SysUser sysUser) {
        //要修改的字段
        KwKcMessage kwKcMessage = new KwKcMessage();
        kwKcMessage.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        kwKcMessage.setUpdateUser(sysUser.getId());
        kwKcMessage.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(KwKcSetting.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(kwKcMessageVo.getKsId())) {
            criteria.andEqualTo("ksId",kwKcMessageVo.getKsId());
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        kwKcMessageDao.updateByExampleSelective(kwKcMessage, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }
}